package com.uenpay.im.ui.old.activity.bankcard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.weight.ZProgressHUD;

import java.util.ArrayList;

/**
 * @Author: SL
 * @Date: 2017/1/17 13:07
 * @CopyRight: http://www.qiyunxin.com
 * @Parameter: TODO
 * @Function: 选择银行卡
 */
public class ChooseBankCardActivity extends Activity {

    private ZProgressHUD mZProgressHUD;
    private BankEntity mBankEntity;
    private ListView listView;
    private ArrayList<BankEntity> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IMApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_list_layout);
        if (getIntent() != null && getIntent().hasExtra("slectedEntity")) {
            mBankEntity = (BankEntity) getIntent().getSerializableExtra("slectedEntity");
        }
        initView();
        initListener();
        myBankCard();
    }

    protected void initView() {
        mZProgressHUD = new ZProgressHUD(this);
        listView = (ListView) findViewById(R.id.listview);
        ((TextView) findViewById(R.id.title_textview)).setText(R.string.choose_bank_card);
    }

    protected void initListener() {
        findViewById(R.id.back_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BankEntity bankEntity = (BankEntity) parent.getAdapter().getItem(position);
                if (bankEntity != null) {
                    Intent intent = new Intent();
                    intent.putExtra("slectedEntity", bankEntity);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    private void myBankCard() {
        mZProgressHUD.show();
        BankCardHandle.myBankCard(new BankCardHandle.IMyBankCardListener() {
            @Override
            public void onMyBankCardResult(int status, String msg, ArrayList<BankEntity> _arrayList) {
                mZProgressHUD.dismiss();
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    arrayList = _arrayList;
                    if (arrayList != null && arrayList.size() > 0) {
                        if (mBankEntity != null) {
                            for (int i = 0, size = arrayList.size(); i < size; i++) {
                                if (arrayList.get(i).id == mBankEntity.id) {
                                    arrayList.get(i).isSelected = true;
                                    break;
                                }
                            }
                        }
                    }
                    listView.setAdapter(new ChooseBankAdapter(ChooseBankCardActivity.this,arrayList));
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        IMApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }
}
